﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.Mvc;
using System.Web.Services;
using VietTrips.Domain.Contexts;
using VietTrips.Domain.Interfaces;
using VietTrips.Domain.Models;
using VietTrips.Domain.Repositories;
using VietTrips.Domain.Services;
using VietTrips.WebUI.Filters;
using VietTrips.WebUI.Models;
using CodeFirstStoredProcs;
using WebMatrix.WebData;

namespace VietTrips.WebUI.Controllers
{
    [InitializeSimpleMembership]
    public class HomeController : Controller
    {
        private readonly ITripRepository _tripRepository = new TripRepository();
        private readonly AdvancedSearchService _advancedSearchService = new AdvancedSearchService();

        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }



        public ActionResult SearchResult(Search search)
        {
            using (var db = new SearchContext())
            {
                // parameter for search   
                // noi xuat phat
                var id = search.DepartPlaceId;
                switch (id)
                {
                    case 9:
                        ViewData["Place"] = "TP Hồ Chí Minh";
                        break;
                    case 11:
                        ViewData["Place"] = "Hà Nội";
                        break;
                    case 12:
                        ViewData["Place"] = "Đà Nẵng";
                        break;
                    case 13:
                        ViewData["Place"] = "Cần Thơ";
                        break;
                    default:
                        ViewData["Place"] = "Tất cả";
                        break;
                }
                // thoi gian xuat phat
                var time = search.DeparTime;
                if (DateTime.Compare(time.Date, new DateTime(1, 1, 1)) == 0)
                {
                    time = new DateTime(1900, 1, 1);
                    ViewData["Time"] = "Tất Cả";
                }
                else
                {
                    ViewData["Time"] = time.ToShortDateString();
                }
                // thoi gian chuyen di
                var duration = search.Duration;
                if (duration > 0 && duration != 9898)
                {
                    ViewData["Duration"] = duration;
                }
                else
                {
                    ViewData["Duration"] = "Tất Cả";
                }
                // noi tham quan
                var area = search.AreaId;
                switch (area)
                {
                    case 1:
                        ViewData["Area"] = "Miền Bắc";
                        break;
                    case 2:
                        ViewData["Area"] = "Miền Trung";
                        break;
                    case 3:
                        ViewData["Area"] = "Miền Đông";
                        break;
                    case 4:
                        ViewData["Area"] = "Miền Tây";
                        break;
                    default:
                        ViewData["Area"] = "Tất Cả";
                        break;
                }

                ViewData["PriceFrom"] = search.PriceFrom;

                ViewData["priceTo"] = search.PriceTo;


                var searchByValue = new Search()
                {
                    DepartPlaceId = id,
                    DeparTime = time,
                    Duration = duration,
                    AreaId = area,
                    PriceFrom = search.PriceFrom,
                    PriceTo = search.PriceTo
                };

                var resultSearches = _advancedSearchService.AdvanceSearch("Price_desc", searchByValue);
                if (resultSearches.Count > 0)
                {
                    ViewData["CountRows"] = resultSearches.Count();
                }
                else
                {
                    ViewData["CountRows"] = 0;
                }               
                ViewData["SearchData"] = search;
                return View(resultSearches);
            }
        }

        public ActionResult TripDetails(int? id)
        {
            

            if (id == null)
            {
                return View("SearchResult");
            }
            else
            {
                var details = _tripRepository.Find((int) id);

                return View(details);
            }
           
        }
    }
}
